package com.alibaba.otter.node.etl.load.loader.mq.rabbit.strategy;

import com.alibaba.otter.node.etl.load.loader.mq.rabbit.model.MessageContext;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@NoArgsConstructor
@AllArgsConstructor
public class BlockUntil implements EndAction {

    private UnblockCondition condition;

    @Override
    public void end(MessageContext context) {
        try {
            if (condition == null) {
                Thread.currentThread().join();
            } else {
                while (!condition.canUnblock(context)) {
                    Thread.sleep(1000);
                }
            }
        } catch (InterruptedException e) {
            log.error("block interrupted.", e);
        }
    }
}
